import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

import org.fluidindia.ria.library.controller.ViewStackNavigationController;
import org.fluidindia.ria.library.event.ViewChangeEvent;
import org.fluidindia.ria.library.remoting.ChannelFactory;
import org.fluidindia.ria.library.remoting.DestinationLocator;



	[Inject]
	public var destinationLocator:DestinationLocator;
	private var navigationController:ViewStackNavigationController;



	private function OnInit() : void
	{
		loadRemotingConfig();
		setupNavigation();
	}



	private function loadRemotingConfig():void
	{
		var loader:URLLoader = new URLLoader();
		var request:URLRequest = new URLRequest("remoting.xml");
		loader.load(request);
		loader.addEventListener(Event.COMPLETE, onLoadXML);
	}

	private function onLoadXML(event:Event):void
	{
		var remoting:XML;
		var loader:URLLoader = URLLoader(event.target);
		remoting = new XML(loader.data);
		
		
		destinationLocator._channelSet = ChannelFactory.createChannelSet(remoting);
	}


	private function setupNavigation():void
	{
		// Setup the navigation controller.
		navigationController = new ViewStackNavigationController(mainApplicationStack);
		
		// Add Navigation Controller to listen to view_change_event.
		addEventListener(ViewChangeEvent.VIEW_CHANGE_EVENT, navigationController.changeView);
	}

